package org.mao.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.mao.forum.entity.ForumPost;
import org.mao.forum.entity.ForumSection;
import org.mao.forum.enums.ForumTaskEnum.ForumEnum;
import org.mao.forum.service.ForumPostService;
import org.mao.forum.service.ForumSectionService;
import org.mao.forum.vo.ForumPostImageVo;
import org.mao.forum.vo.ForumPostVo;
import org.mao.forum.vo.ForumSectionVo;
import org.mao.util.Page;
import org.mao.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.google.gson.Gson;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/1")
public class LwgodController {

	private Logger logger = Logger.getLogger(LwgodController.class);

	@Autowired
	private ForumSectionService forumSectionService;

	@Autowired
	private ForumPostService forumPostService;

	@RequestMapping(value = "/1/list")
	@ResponseBody
	public String sectionlist(HttpServletRequest request, HttpServletResponse response) {
		String resultJSON = "";
		Gson gson = new Gson();
		Map<String, Object> map = Maps.newHashMap();
		try {
			List<ForumSectionVo> sectionList = forumSectionService.listSection(ForumEnum.LWGOD.getType());
			map.put("list", sectionList);
			resultJSON = StringUtil.toHex(gson.toJson(map));
			logger.debug("resultJSON: " + resultJSON);
		} catch (Exception e) {
			logger.error("", e);
		}
		return resultJSON;
	}

	@RequestMapping(value = "/2/list")
	@ResponseBody
	public String postlist(HttpServletRequest request, HttpServletResponse response, Integer sectionId, Integer currentPage) {
		String resultJSON = "";
		Gson gson = new Gson();
		Map<String, Object> map = Maps.newHashMap();
		try {
			ForumSection forumSection = new ForumSection();
			forumSection.setId(sectionId);
			forumSection = forumSectionService.get(forumSection);

			Page page = new Page();
			page.setCurrentPage(currentPage);
			List<ForumPostVo> postList = forumPostService.queryBySectionId(sectionId, page);
			page.getTotalPage();
			map.put("forumSection", forumSection);
			map.put("postList", postList);
			map.put("page", page);
			resultJSON = StringUtil.toHex(gson.toJson(map));
			logger.debug("resultJSON: " + resultJSON);
		} catch (Exception e) {
			logger.error("", e);
		}
		return resultJSON;
	}

	@RequestMapping(value = "/2/detail")
	@ResponseBody
	public String postDetail(HttpServletRequest request, Integer postId) {
		String resultJSON = "";
		Gson gson = new Gson();
		Map<String, Object> map = Maps.newHashMap();
		try {
			ForumPost forumPost = new ForumPost();
			forumPost.setId(postId);
			forumPost = forumPostService.get(forumPost);
			List<ForumPostImageVo> imageList = forumPostService.queryByPostId(postId);
			map.put("content", forumPost.getContent());
			map.put("imageList", imageList);
			resultJSON = StringUtil.toHex(gson.toJson(map));
			logger.debug("resultJSON: " + resultJSON);
		} catch (Exception e) {
			logger.error("", e);
		}
		return resultJSON;
	}

	@RequestMapping(value = "/2/saveImage")
	@ResponseBody
	public JSONObject saveImage(HttpServletRequest request, Integer postImageId) {
		JSONObject json = new JSONObject();
		try {
			if (forumPostService.saveImage(postImageId)) {
				json.put("result", 1);
			} else {
				json.put("result", 0);
			}
		} catch (Exception e) {
			logger.error("", e);
			json.put("result", 0);
		}
		return json;
	}

	@RequestMapping(value = "/2/saveTorrent")
	@ResponseBody
	public JSONObject saveTorrent(HttpServletRequest request, Integer postId) {
		JSONObject json = new JSONObject();
		try {
			if (forumPostService.saveTorrent(postId)) {
				json.put("result", 1);
			} else {
				json.put("result", 0);
			}
		} catch (Exception e) {
			logger.error("", e);
			json.put("result", 0);
		}
		return json;
	}

}
